﻿<%@ control language="C#" autoeventwireup="true" inherits="UserControl_ManagerAdmin, App_Web_pmcgjoc4" %>
<%@ Import Namespace="DacSanMienTayOBJ" %>

<%@ Import Namespace="System.Collections.Generic" %>
<script type="text/javascript" src="Scripts/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="<%=Utility.modHost %>/Scripts/jquery.simplemodal.js"></script>

<style>
#simplemodal-overlay {background-color:#000;}
#simplemodal-container {height:378px; width:619px; color:#bbb; background:url(../Images/bg_login.png) no-repeat center; padding:12px; position:relative;}
#simplemodal-container .simplemodal-data {position:relative;}
#simplemodal-container a.modalCloseImg {background:url(../Images/x.png) no-repeat; width:25px; height:29px; display:inline; z-index:3200; position:absolute; top:0px; right:0px; cursor:pointer;}

#simplemodal_container_uploadimage {height:490px; width:700px; color:#bbb;background-color:White;border:5px;border-style:solid;border-color:#ce5c00; position:relative;margin-top:50px}
#simplemodal_container_uploadimage .simplemodal-data {position:relative;}
#simplemodal_container_uploadimage a.modalCloseImg {background:url(../Images/x.png) no-repeat; width:25px; height:29px; display:inline; z-index:3200; position:absolute; top:-16px; right:-16px; cursor:pointer;}

</style>


<div>
<span>
Tên đăng nhập
</span>
<input type="text" style="width:300px" id="txtUserName" name="txtUserName">
<br />
<br />
<span>
Mật khẩu
</span>
<input type="password" style="width:300px; position:relative; left:30px" id="txtPassWord" name="txtPassWord">
<br />
<br />
<span> Chọn quyền admin</span>
<%
    
try
    {
        int stt=1;
        List<GroupAdmin> listGroup = AdminBusiness.ListGroupAdmin();
        
        foreach (GroupAdmin groupAdmin in listGroup)
        {
            
            %>
            <input type="checkbox" id="cbGroupAdmin<%=stt %>" value="<%=groupAdmin.GroupID %>" /><%=groupAdmin.GroupName %>
            <%
            stt++;
        }
        %>
        <input type="hidden" id="hdCountGroup" value="<%=listGroup.Count %>" />
        <%
    }
    catch(Exception ex)
    {
        Utilitys.WriteLog("Groupadmin : " +ex.Message);
    }
     %>

     <br />
<asp:Button ID="cmdInsert" runat="server" Text="Thêm mới" 
    OnClientClick="return CheckRegullar()" onclick="cmdInsert_Click" />

</div>
<br />
<br />
<input type="hidden" id="hdGroupAdmin" name="hdGroupAdmin" />
<div>
<span>
Danh Sách admin

</span>
<br />
<br />
<table>
<tr>
<td style="width:80px">Mã Admin</td>
<td style="width:200px">UserName</td>
<td style="width:200px">Password</td>
<td style="width:100px">Tình trạng</td>
<td style="width:100px">Cập nhật</td>
<td style="width:100px">Lock/Unlock</td>
</tr>

<tbody id="bodyAdmin">
<%
    try
    {
        List<Admin> list = AdminBusiness.ListAllAdmin();
        if (list != null)
        {
            int vt = 0;
            foreach (Admin admin in list)
            {
                string status = "";
                if (admin.IsVisible == 1)
                {
                    status = "Active";
                }
                else
                {
                    status = "Inactive";
                }
                
                
     %>
<tr id="trVtAdmin<%=vt %>">
    <td><label id="lbAdminID<%=vt %>" title="<%=admin.AdminID %>"><%=admin.AdminID%></label></td>
    <td><input type="text" style="width:200px" value="<%=admin.UserName %>" id="txtUserNameUpdate<%=vt %>" /> </td>
    <td><input type="password" style="width:180px" value="<%=admin.Password %>" id="txtPasswordUpdate<%=vt %>" /> </td>
    <td><%=status %></td>
    <td><input type="button" value="Cập nhật" onclick="UpdateUserName('<%=admin.AdminID %>','<%=vt %>','<%=admin.IsVisible %>')" /> </td>
    <td>
    <%
        if (admin.IsVisible == 1)
        {
        %>
            <input type="button" value="Lock" onclick="LockAndUnclockAdmin('<%=admin.AdminID %>','<%=vt %>','lock','<%=admin.IsVisible %>')" />
            <%
        }
        else
        {
        %>
            <input type="button" value="UnLock" onclick="LockAndUnclockAdmin('<%=admin.AdminID %>','<%=vt %>','unlock','<%=admin.IsVisible %>')" />
            <%
        }
                 %>
                 </td>
                 
</tr>

<%
    vt++;
    }
        }
    }
    catch(Exception ex)
    {
        Utilitys.WriteLog(ex.Message);
    }
     %>
</tbody>
</table>
</div>
<script type="text/javascript">

    function CheckRegullar() {
        var userName = $('#txtUserName').val();
        var pass = $('#txtPassWord').val();
        var countGroup = $('#hdCountGroup').val();
        var groupAdmin = '';
        var checkReg = true;
        if (userName == '') {
            alert('Vui lòng nhập username');
            checkReg = false;

        }
        if (pass == "") {
            alert('Vui lòng nhập password');
            checkReg = false;
        }

        var arrGroup = new Array();
        var checkGroup = 'false';
        for (var i = 1; i <= countGroup; i++) {
            if (document.getElementById('cbGroupAdmin' + i).checked == true) {
                checkGroup = 'true';
                groupAdmin = $('#cbGroupAdmin' + i).val();
                arrGroup.push(groupAdmin);
            }
        }

        if (checkGroup == 'false') {
            checkReg = false;
            alert('Vui lòng chọn quyền');
        }
        alert(checkGroup);
        alert(checkReg);
        if (!checkReg) {
            return false;
        }
        else {
            $('#hdGroupAdmin').val(arrGroup);
          
            return true;
        }
    }

    function UpdateUserName(adminID, vt, visible) {
        var txtUserName = '#txtUserNameUpdate' + vt;
        var txtPass = '#txtPasswordUpdate' + vt;
        var trAdminID = "#trVtAdmin" + vt;
        var userName = $(txtUserName).val();
        var pass = $(txtPass).val();
        var lbAdminID = '#lbAdminID' + vt;
        var adminID = $(lbAdminID).text();
        var load = '<img src="<%=Utility.modHost %>/Images/loading.gif" id="imgLoad" alt="" style="margin-left:auto;margin-right:auto;margin-top:53%;margin-left:135%;width:40px;height:40px" />';
        $(trAdminID).html(load);
        $.ajax({
            type: "post",
            url: "<%=Utility.modHost %>/Proccess/UpdateAdvNamePosition.aspx",
            async: false,
            data: { adminID: adminID, userName: userName,pass:pass, vt: vt, visible: visible },
            success: function (data2) {
                if (data2 == "errorupdate") {
                    alert('Lỗi cập nhật. Vui lòng liên hệ admin');
                }
                else {
                    document.getElementById('trVtAdmin' + vt).innerHTML = data2;
                }
            }
        });

    }

    function LockAndUnclockAdmin(advID, vt, typeLock, visible) {
        var txtUserName = '#txtUserNameUpdate' + vt;
        var txtPass = '#txtPasswordUpdate' + vt;
        var trAdminID = "#trVtAdmin" + vt;
        var userName = $(txtUserName).val();
        var pass = $(txtPass).val();
        var lbAdminID = '#lbAdminID' + vt;
        var adminID = $(lbAdminID).text();
        var load = '<img src="<%=Utility.modHost %>/Images/loading.gif" id="imgLoad" alt="" style="margin-left:auto;margin-right:auto;margin-top:53%;margin-left:135%;width:40px;height:40px" />';
        $(trAdminID).html(load);
        $.ajax({
            type: "post",
            url: "<%=Utility.modHost %>/Proccess/LockAndUnclockAdvPosition.aspx",
            async: false,
            data: { adminID: adminID, typeLock: typeLock, vt: vt, visible: visible, userName: userName, pass: pass },
            success: function (data2) {
                if (data2 == "errorlock") {
                    alert('Không khóa được. Vui lòng liên hệ admin');
                }
                else if (data2 == "errorunlock") {
                    alert('Không mở khóa được. Vui lòng liên hệ admin');
                }
                else {
                    document.getElementById('trVtAdmin' + vt).innerHTML = data2;
                }
            }
        });
    }
</script>